package com.example.springbootstartermqttprovider.mudan;

import okhttp3.*;
import org.springframework.util.DigestUtils;

import java.io.IOException;
import java.time.Instant;

public class TokenFuNanUtils {
    public static void main(String[] args) {
        long timestamp = Instant.now().toEpochMilli();
        String appId="40288aca96b444510196c74a284d0026";
        String sign = new TokenFuNanUtils().getSign(appId,timestamp);
        System.out.println("请求passId:"+appId);
        System.out.println("请求timestamp:"+timestamp);
        System.out.println("请求sign:"+sign);

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\r\n    \"virtualFence\": \"POLYGON((115.61200794 32.64003561,115.61205988 32.64002551,115.61201215 32.63984974,115.6119602 32.63985983,115.61200794 32.64003561))\",\r\n    \"name\": \"TEST 围栏\",\r\n    \"stationId\": \"1244843217025081344\"\r\n}");
        Request request = new Request.Builder()
                .url("http://8.154.33.12/generic/area/updateSiteInfo")
                .method("POST", body)
                .addHeader("appId", appId)
                .addHeader("timestamp", String.valueOf(timestamp))
                .addHeader("sign", sign)
                .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .addHeader("Content-Type", "application/json")
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                // 获取响应体内容
                String responseData = response.body().string();
                System.out.println("响应成功: " + responseData);
            } else {
                System.out.println("请求失败，状态码: " + response.code());
            }
        } catch (IOException e) {
            System.out.println("网络错误: " + e.getMessage());
        }

    }

    public String getSign(String appId,long tmestamp){
        String appkeyStr = appId + "_" + tmestamp + "_" + appId;
        String signData = DigestUtils.md5DigestAsHex(appkeyStr.getBytes());
        return signData;
    }
}
